package com.llsydn.a06copyviewpager;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * @author lilinshen
 * @title 仿viewpager控件
 * @description 请填写相关描述
 * @date 2018/10/16 15:33
 */
public class MainActivity extends Activity {
    private int[] ids = {R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6};
    private MyViewPager myviewpager;
    private RadioGroup rg_main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏标题
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化值
        myviewpager = findViewById(R.id.myviewpager);
        rg_main = findViewById(R.id.rg_main);
        for (int i = 0; i < ids.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(ids[i]);
            //添加到MyViewPager的view中
            myviewpager.addView(imageView);
        }
        for (int i = 0; i < myviewpager.getChildCount(); i++) {
            RadioButton button = new RadioButton(this);
            button.setId(i);
            if (i == 0) {
                button.setChecked(true);
            }
            //添加到RadioGroup中
            rg_main.addView(button);
        }
        //設置radiogroup选中状态的变化
        rg_main.setOnCheckedChangeListener((group, checkId) -> {
            //checkId 0-5 之间
            //根据下标位置定位到具体的某个页面
            myviewpager.scrollToPage(checkId);
        });

        //设置监听页面的改变（自定义的接口）
        myviewpager.setOnPagerChangListener((position -> {
            rg_main.check(position);
        }));
    }
}
